<!--
 * @Author: chunlai
 * @Date: 2023-12-07 08:51:56
 * @LastEditTime: 2025-11-20 15:16:32
 * @LastEditors: minsheng_insurance_agency_zhoucl
 * @FilePath: \01.latest-short-insure\src\App.vue
 * @Description: 
-->
<template>
  <div>
    <keep-alive include="minsheng">
      <router-view v-if="pageLoad"></router-view>
    </keep-alive>
    <MyLoading :show="vLoad" :text="loadTxt" />
    <MsgDescMask v-if="filedDescCon" />
    <div v-if="disabled" class="disabledPage" @click="noAuthAction"></div>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import { delNull } from "@/utils/functions.js";
import { GetRiskData } from "@/translate/risks/insert.js";
import { rrwebMixin } from "@/mixins/rrweb.js";
export default {
  name: "InsureApp",
  components: {},
  data() {
    return {
      staffErrMsg: "",
      disabled: false,
      pageLoad: false,
      defaultPhoneHeight: "", //屏幕默认高度
      nowPhoneHeight: "", //屏幕现在的高度
    };
  },
  computed: {
    ...mapGetters({
      vLoad: "vLoad",
      loadTxt: "loadTxt",
      isOnline: "varietyInfo/isOnline",
      inAppFlag: "varietyInfo/inAppFlag",
      filedDescCon: "common/filedDescCon",
    }),
  },
  watch: {
    $route(to, from) {
      window.scrollTo(0, 0);
      // this.sendDatas(this.events);
    },
    nowPhoneHeight() {
      if (this.defaultPhoneHeight != this.nowPhoneHeight) {
        //手机键盘被唤起了。
        // alert("手机键盘被唤起了。")
        //写软键盘唤起你需要做的操作
        //如this.tabbar = true
      } else {
        //  alert("手机键盘被关闭了。")
        document.activeElement.blur();
        //手机键盘被关闭了。
        //写软键盘关闭你需要做的操作
        //如this.tabbar = false
      }
    },
  },
  mixins: [rrwebMixin],
  async created() {
    this.$store.commit("updateLoad", true);
    /**岗位号 */
    const workNum = this.$getDataFromUrl("workNum");
    if (workNum) {
      this.$store.commit("SET_WORK_NUM", workNum);
    }
    const comName = this.$getDataFromUrl("company");
    if (comName) {
      this.$store.commit("SET_ENG_GYS", comName);
    }
    const mcode = this.$getDataFromUrl("mcode");
    /**险种配置重置*/
    if (this.mainRiskCode !== mcode) {
      this.$store.commit("SET_ORDER_NUM", "");
      this.$store.commit("varietyInfo/SET_RISK_CONFIG", -1);
    }
    if (mcode) {
      this.$store.commit("SET_MAIN_CODE", mcode);
    }
    const msChal = this.$getDataFromUrl("msChannel");
    if (msChal) {
      this.$store.commit("insert/SET_MS_CHANNEL", msChal);
    }
    let proIdCode = this.$getDataFromUrl("productCode");
    if (proIdCode) {
      this.$store.commit("SET_ID_CODE", proIdCode);
    }
    let userId = this.$getDataFromUrl("userId");
    if (userId) {
      this.$store.commit("SET_YUN_UID", userId);
    }
    let instype = this.$getDataFromUrl("instype");
    if (instype) {
      this.$store.commit("SET_INS_TYPE", instype);
    }
    // 续保订单号
    let preOrderNum = this.$getDataFromUrl("preOrderNum");
    if (preOrderNum) {
      this.$store.commit("SET_PREORDER_NUM", preOrderNum);
    }
    let res2 = await this.$store.dispatch("json/getBaseDataJson", "page");
    this.$store.dispatch("json/getBaseDataJson", "dictionary");
    this.initPage();
    let that = this;
    window.addEventListener("popstate", function (event) {
      console.log("popstate: " + JSON.stringify(event));
      that.sendDatas(that.events);
    });
    // console.log(this.$GlobalFuns.userAge("2006-09-04"))
  },
  mounted() {
    this.scrollIntoView();
  },
  methods: {
    async initPage() {
      this.checkStaff();
      let apiPams = {
        mainRiskCode: this.mainRiskCode,
        workNum: this.workNum,
      };
      if (this.proIdCode) {
        apiPams.productCode = this.proIdCode;
      }
      let data = await this.$store.dispatch("apis/comAdminApiRequest", {
        apiParams: apiPams,
        apiName: "getVarietyInfo",
        toast: true,
      });
      if (data.code != 1) return;
      /**获取订单号 */
      const preOrderNum = this.$getDataFromUrl("preOrderNum");
      if (this.instype == "2" && preOrderNum) {
        let xbParams = {
          ...this.apiComParams,
        };
        xbParams.orderNum = preOrderNum;
        const data2 = await this.$store.dispatch(
          "apis/getBillDetails",
          xbParams
        );
        this.renewlBackToStore(data2);
      }
      /**获取订单号 */
      let orderNum = "";
      if (this.$getDataFromUrl("orderNum")) {
        orderNum = this.$getDataFromUrl("orderNum");
      }
      // 获取并设置订单号
      if (orderNum) {
        this.$store.commit("SET_ORDER_NUM", orderNum);
        const res2Data = await this.$store.dispatch(
          "apis/getBillDetails",
          this.apiComParams
        );
        this.backDetailToStore(res2Data);
      } else if (!this.orderNum) {
        this.getOrderNum();
      }
      this.$store.commit("updateLoad", false);
      let hashurl = window.location.hash;
      if (hashurl.indexOf("#/index") == 0 || hashurl == "#/") {
        /**页面地址 */
        const ptype = this.$getDataFromUrl("ptype");
        if (ptype) {
          this.$router.replace(ptype);
        }
      }
      this.$nextTick(() => {
        this.pageLoad = true;
        this.openRrweb();
      });
    },
    //获取订单号
    getOrderNum() {
      let params = {
        mainRiskCode: this.$getDataFromUrl("mcode"),
        workNum: this.$getDataFromUrl("workNum"),
      };
      if (this.$getDataFromUrl("productCode")) {
        params.productCode = this.$getDataFromUrl("productCode");
      }
      // 新增推广业务员
      if (this.$getDataFromUrl("tgWorkNum")) {
        params.insureWorkNum = this.$getDataFromUrl("tgWorkNum");
      }
      this.$http
        .post("/ms-insure/api/common/aquireOrderNum", params, {
          json: true,
        })
        .then(({ data }) => {
          if (data.code == 1) {
            this.$store.commit("SET_ORDER_NUM", data.orderNum);
            this.sendDatas(this.events);
          } else {
            this.$dialog.alert({
              message: data.msg,
            });
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
    /**检查代理人资质获取业务员信息 */
    checkStaff() {
      if (["2"].includes(this.instype)) return;
      let params = {
        mainRiskCode: this.mainRiskCode,
        workNum: this.workNum,
      };
      if (this.proIdCode) {
        params.productCode = this.proIdCode;
      }
      this.disabled = false;
      this.$http
        .post("/ms-insure/api/common/checkStaff", params, {
          json: true,
        })
        .then(({ data }) => {
          if (data.code == "1") {
            this.$store.commit("agent/SET_STATE_FIELD", data.agent);
            const sale_auth = data.agent.hasSaleAuth || "0";
            if (sale_auth == "1") {
              this.disabled = true;
            }
          } else {
            this.$store.commit("agent/SET_STATE_FIELD", {
              hasSaleAuth: "1",
            });
            this.$store.commit("home/SET_READ_POP", "1");
            // 无资质禁止操作
            this.$dialog
              .alert({
                message: data.msg,
              })
              .then(() => {
                this.staffErrMsg = data.msg;
                this.disabled = true;
              });
          }
        });
    },
    /**无权限早操作*/
    noAuthAction() {
      if (this.disabled) {
        this.$baseAlert(
          `${
            this.staffErrMsg ||
            "对不起，您当前无权限销售此产品，请联系人管开放销售权限"
          }`
        );
      }
    },
    backDetailToStore(payload) {
      let { appnt, cont, insureds } = payload;
      // 核保禁止修改--->判断页面跳转/能否修改信息
      if (payload.flag !== void 0) {
        if (["-1", "1", "3", "4", "5", "6"].includes(payload.flag)) {
          this.$store.commit("insert/SET_FORBID_ALTER", true);
        }
      }
      if (payload.userId) {
        this.$store.commit("SET_YUN_UID", payload.userId);
      }
      if (appnt) {
        this.$store.commit("insert/UPDATE_APPLICANT", delNull(appnt));
      }
      if (cont) {
        // 保单状态--->确认页/支付页,头部信息显示
        if (cont.orderStatus !== void 0) {
          this.$store.commit("insert/SET_INSURE_FLAG", {
            code: cont.orderStatus || "",
            msg: cont.orderRemark || "",
          });
        }
        this.$store.commit("insert/UPDATE_PAYINFO", delNull(cont));
      }
      if (insureds) {
        if (insureds[0]) {
          let risks = insureds[0].risks;
          if (risks) {
            this.$store.commit("insert/SET_RECALL", "0");
            let risk = GetRiskData(risks);
            this.$store.commit("risks/SET_RISKS_DATA", risk);
          }
          let insured = JSON.parse(JSON.stringify(insureds));
          delete insured.bnf;
          delete insured.risks;
          this.$store.commit("insert/SET_ASSURED", delNull(insured));
        }
      }
    },
    renewlBackToStore(payload) {
      if (this.disabledEdit) return;
      let { appnt, insureds, cont } = payload;
      if (payload.userId) {
        this.$store.commit("SET_YUN_UID", payload.userId);
      }
      if (appnt) {
        this.$store.commit("insert/UPDATE_APPLICANT", delNull(appnt));
      }
      if (cont && cont.expiryDate) {
        this.$store.commit("renewal/SET_EXPIRY_DATE", cont.expiryDate);
      }
      if (insureds && insureds[0]) {
        let risks = insureds[0].risks;
        if (risks) {
          this.$store.dispatch("renewal/initPreRisk", risks);
        }
        let insured = JSON.parse(JSON.stringify(insureds));
        delete insured.bnf;
        delete insured.risks;
        // insured.forEach((e) => {
        //   e.healthyStatus = "";
        // });
        this.$store.commit("insert/SET_ASSURED", delNull(insured));
      }
    },
  },
};
</script>
<style lang="less" scoped>
.disabledPage {
  position: fixed;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
  z-index: 1990;
}
</style>
